#include "BlockQueue.hpp"
#include "Task.hpp"
#include <ctime>

int myadd(int x, int y)
{
    return x + y;
}

void* consumer(void* args)
{
    BlockQueue<Task> *bqueue = (BlockQueue<Task>*)args;
    while (true)
    {
        //int a;
        // 获取任务
        Task t;
        bqueue->pop(&t);
        // 完成任务
        std::cout  << "consumer : " << t.x_ << " + " << t.y_ << " = " << t() << std::endl;
        sleep(1);
    }

    return nullptr;
}

void* productor(void* args)
{
    BlockQueue<Task> *bqueue = (BlockQueue<Task>*)args;

    //int a = 1;
    while (true)
    {
        // 制作任务
        int x = rand()%10 + 1;
        usleep(rand()%1000);
        int y = rand()%5 + 1;

        Task t(x, y, myadd);
        // 生产任务
        bqueue->push(t);
        // 输出任务
        std::cout  << "productor : " << t.x_ << " + " << t.y_ << "=?" << std::endl;
        sleep(1);
    }

    return nullptr;
}

int main()
{
    srand((unsigned long)time(nullptr) ^ getpid() ^ 123);
    BlockQueue<Task> *bqueue = new BlockQueue<Task>();

    pthread_t c[2], p[2];
    pthread_create(c, nullptr, consumer, bqueue);
    pthread_create(c+1, nullptr, consumer, bqueue);
    pthread_create(p, nullptr, productor, bqueue);
    pthread_create(p+1, nullptr, productor, bqueue);

    pthread_join(c[0], nullptr);
    pthread_join(c[1], nullptr);
    pthread_join(p[0], nullptr);
    pthread_join(p[1], nullptr);

    return 0;
}